﻿//-----------------------------------------------------------------------
// <copyright file="EventInfoRepository.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: EventInfoRepository.cs
// * history : created by qinchaoyue 2018-03-06 11:28:45
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Linq;
using System.Text.RegularExpressions;
using Slwy.Data.Entity.Interfaces;
using Slwy.Data.Entity.SqlSugarExtensions;
using Slwy.EventBus.Interfaces;
using Slwy.EventBus.Models;
using Slwy.KeyIDMaker;
using Slwy.MySql.Contracts;
using Slwy.Utility.Extensions;

namespace Slwy.EventBus.Repository
{
    public class EventInfoRepository : IEventInfoRepository
    {
        // public Action<object> OnSetConnection { get; set; }

        public IEditRepository<EventInfoEntity, string> EventInfoRepo { get; set; }
        public IMySqlConnection MySqlConnection { get; set; }

        private void SetConnection(string connectionString)
        {
            // 很尴尬, 当初设计底层时只能使用 数据库配置名称, 而不能使用数据库连接, 所以这里要反着使用value搜索key.
            var connectionName = string.Empty;
            var reges = new Regex("(?<=database=).*?;");
            var database = reges.Match(connectionString).Value;
            foreach (var m in MySqlConnection.GetConnections().ToList())
            {
                var mDatabase = reges.Match(m.ConnectionString).Value;
                if (database.ToUpper().Equals(mDatabase.ToUpper()))
                {
                    connectionName = m.Name;
                    break;
                }
            }
            // var connection = connections.FirstOrDefault(m => m.ConnectionString == connectionString);

            EventInfoRepo.SetConnection(connectionName, connectionName.ToUpper().Replace("WRITE", "READE"));
            // OnSetConnection?.Invoke(EventInfoRepo);
        }


        private void SetConnection<T>(IBaseRepository<T> repo)
            where T : class, new()
        {
            var connectionString = repo.Client.Ado.Connection.ConnectionString;
            SetConnection(connectionString);
        }


        public bool InsertEventInfo<T>(IBaseRepository<T> repo, EventInfoEntity entity)
            where T : class, new()
        {
            SetConnection(repo);
            entity.KeyID = entity.KeyID.IsNullOrEmpty() ? KeyIDMakerHalder.NewKeyID(BizType.VipHall) : entity.KeyID;
            try
            {
                return EventInfoRepo.Insert(entity).TableYYYYMM(entity.KeyID).ExecuteCommand() > 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public bool UpdateEventInfoForPosted<T>(IBaseRepository<T> repo, string keyId)
            where T : class, new()
        {
            SetConnection(repo);
            return EventInfoRepo.Updateable()
                .UpdateColumns(m => new EventInfoEntity
                {
                    PostDateTime = DateTime.Now,
                    IsPostd = true
                })
                .TableYYYYMM(keyId)
                .Where(m => m.KeyID == keyId)
                .ExecuteCommand() > 0;
        }

        public bool UpdateEventInfoForPosted(string connectionString, string keyId)
        {
            SetConnection(connectionString);
            return EventInfoRepo.Updateable()
                       .UpdateColumns(m => new EventInfoEntity
                       {
                           PostDateTime = DateTime.Now,
                           IsPostd = true
                       })
                       .TableYYYYMM(keyId)
                       .Where(m => m.KeyID == keyId)
                       .ExecuteCommand() > 0;

        }

        public bool UpdateEventInfoErrorMessage(string connectionString, string keyId, string errorMessage)
        {
            SetConnection(connectionString);

            return EventInfoRepo.Updateable()
                       .UpdateColumns(m => new EventInfoEntity
                       {
                           ErrorMessage = errorMessage,
                       })
                       .TableYYYYMM(keyId)
                       .Where(m => m.KeyID == keyId)
                       .ExecuteCommand() > 0;
        }
    }
}
